package cn.itcast.mq.test;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.sql.Time;
import java.time.LocalDateTime;

/**
 * @author 嘉德罗斯
 * @date 2021/10/18
 */
@Component
public class SpringRabbitListener {

    @RabbitListener(queues = "A")
    public  void listen1(String msg) throws InterruptedException {
        System.out.println("消息1"+msg+"..."+LocalDateTime.now());
        Thread.sleep(20);
    }


    @RabbitListener(queues = "A")
    public  void listen2(String msg) throws InterruptedException {
        System.err.println("消息2"+msg+"..."+LocalDateTime.now());
        Thread.sleep(200);
    }


    @RabbitListener(queues = "Q1")
    public  void Fanout1(String msg) throws InterruptedException {
        System.out.println("Q1"+msg+"..."+LocalDateTime.now());
    }


    @RabbitListener(queues = "Q2")
    public  void Fanout2(String msg) throws InterruptedException {
        System.out.println("Q2"+msg+"..."+LocalDateTime.now());
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "D1"),
            exchange = @Exchange(name = "Direct",type = ExchangeTypes.DIRECT),
            key = {"red","yellow"}
    ))
    public void Direct1(String msg){
        System.out.println("D1..."+msg+"...");
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "D2"),
            exchange = @Exchange(name = "Direct",type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
    ))
    public void Direct2(String msg){
        System.out.println("D2..."+msg+"...");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "T1"),
            exchange = @Exchange(name = "Topic",type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void topic1(String msg){
        System.out.println("T1..."+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "T2"),
            exchange = @Exchange(name = "Topic",type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void topic2(String msg){
        System.out.println("T2..."+msg);
    }
}
